﻿using System;
using UnityEditor;
using UnityEngine.UIElements;
using TextField = winS.UnityEditor.UIElement.TextField;

namespace winS.UnityEditor.GUI
{
    public class FolderPicker : VisualElement
    {
        public event Action<string> onValueChanged;

        public string panelTitle { get; set; } = "FolderPicker";

        public string label { get => textField.label.text; set => textField.label.text = value; }

        private readonly TextField textField;

        internal FolderPicker(GUIFactory guiFactory)
        {
            style.flexDirection = FlexDirection.Row;
            Add(textField = guiFactory.CreateTextField(newValue => onValueChanged?.Invoke(newValue)));
            Add(guiFactory.CreateButton("浏览", () =>
            {
                string result = EditorUtility.OpenFolderPanel(panelTitle, string.Empty, string.Empty);
                if (result != string.Empty) textField.value = result;
            }));

            textField.style.flexGrow = 1f;
        }

        public void SetValueWithoutNotify(string value)
        {
            textField.SetValueWithoutNotify(value);
        }
    }
}